<template>
  <view class="page">
    <Navbar title="我的订单" occupy scroll-top="40" scroll-bg-color="#15161c">
      <u-icon slot="left" name="arrow-left" :color="scrollTop >= 40 ? '#222' : '#fff'" />
    </Navbar>
    <view
      class="sticky"
      :style="{
        background: scrollTop >= 20 ? '#15161c' : 'transparent',
      }"
    >
      <Tabs :list="tabList" :current="current" @change="changeTab" />
    </view>
    <Bill-item v-for="(i, k) in dataList" :key="k" :data="i" />
    <u-loadmore :status="status" height="40" />
  </view>
</template>

<script>
import { BillItem } from "@/pages/my/cp/BillItem";
import { getChargeList, getStopList } from "@/request/myApi.js";
export default {
  components: { BillItem },
  data() {
    return {
      current: 0, // 当前下标
      queryForm: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0, // 总条数
      tabList: [{ name: "充电记录" }, { name: "停车记录" }],
      scrollTop: 0, // 距离顶部距离
      dataList: [], // 数据列表
      status: "loadmore", // 加载状态
    };
  },

  mounted() {
    this.handleQuery();
  },
  onReachBottom() {
    if (this.total <= this.dataList.length) return;
    this.queryForm.pageNum++;
    this.handleQuery();
  },
  onPullDownRefresh() {
    this.queryForm.pageNum = 1;
    this.handleQuery();
  },
  onPageScroll(scroll) {
    this.scrollTop = parseInt(scroll.scrollTop);
    uni.$emit("onPageScroll", scroll.scrollTop);
  },
  methods: {
    // 处理查询
    handleQuery() {
      if (this.current == 0) {
        this.getChargeRecord();
      } else {
        this.getStopRecord();
      }
    },
    // 切换菜单
    changeTab(val) {
      this.current = val.index;
      this.queryForm.pageNum = 1;
      this.handleQuery();
    },
    // 获取充电记录
    getChargeRecord() {
      this.status = "loading";
      getChargeList(this.queryForm)
        .then((res) => {
          if (res.code == 200) {
            let { pageNum } = this.queryForm;
            this.dataList = pageNum == 1 ? res.rows : this.dataList.concat(res.rows);
            this.total = res.total;
          } else {
            uni.showToast({
              title: res.msg || "服务器错误",
              icon: "none",
              mask: true,
            });
          }
        })
        .finally(() => {
          this.status = this.total <= this.dataList.length ? "nomore" : "loadmore";
          uni.stopPullDownRefresh();
        });
    },
    // 获取停车记录
    getStopRecord() {
      this.status = "loading";
      getStopList(this.queryForm)
        .then((res) => {
          if (res.code == 200) {
            let { pageNum } = this.queryForm;
            this.dataList = pageNum == 1 ? res.rows : this.dataList.concat(res.rows);
            this.total = res.total;
          } else {
            uni.showToast({
              title: res.msg || "服务器错误",
              icon: "none",
              mask: true,
            });
          }
        })
        .finally(() => {
          this.status = this.total <= this.dataList.length ? "nomore" : "loadmore";
          uni.stopPullDownRefresh();
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  width: 100%;
  min-height: 100vh;
  background: radial-gradient(48.31% 108.99% at 100% 1.17%, #142527 0%, #15161c 100%);
  position: relative;
  .scroll-view {
    height: calc(100vh - 176rpx - var(--status-bar-height) - var(--window-top));
  }
  .sticky {
    position: sticky;
    top: calc(66rpx + var(--status-bar-height));
    z-index: 999;
  }
}
</style>
